package com.sgb.member.ui.adapter;

import android.app.Activity;
import android.content.Intent;
import android.icu.text.DecimalFormat;
import android.net.Uri;
import android.os.Build;
import android.text.Html;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.RequiresApi;

import com.sgb.member.BR;
import com.sgb.member.MemberConfig;
import com.sgb.member.R;
import com.sgb.member.databinding.ItemOtherLogistics2Binding;
import com.sgb.member.entity.MemberLogisticsEntity;
import com.sgb.member.util.MCargoConstant;
import com.sgb.member.util.MVehicleConstant;
import com.sgb.member.weight.MemberCallback;
import com.swgk.core.dialog.RoundCornerDialog;
import com.swgk.core.recyclerview.adapter.BaseRecycleAdapter;
import com.swgk.core.recyclerview.adapter.ItemViewHolder;
import com.swgk.core.util.MToast;
import com.swgk.core.util.NumberUtil;
import com.swgk.core.util.SharedPreferenceUtil;
import com.swgk.core.util.StringUtil;

import java.util.List;

/**
 * 物流货运-会员货运
 * @param <T>
 */
public class WuLiuMemberAdapter2<T> extends BaseRecycleAdapter<T> {

    MemberCallback memberCallback;

    public WuLiuMemberAdapter2(Activity activity, int layoutId, List<T> datas) {
        super(activity, layoutId, datas);
    }

    public WuLiuMemberAdapter2(Activity activity, int layoutId, List<T> datas, MemberCallback memberCallback) {
        super(activity, layoutId, datas);
        this.memberCallback = memberCallback;
    }

    @Override
    public void convert(ItemViewHolder holder, final T t) {
        logisticsGoods(holder, t);
    }

    private IChildClick<T> iChildClick;

    public void onIChildClick(IChildClick<T> iChildClick) {
        this.iChildClick = iChildClick;
    }

    public interface IChildClick<T> {
        void onClick(T tenderListEntity);
    }

    // 物流货物 -会员货运
    @RequiresApi(api = Build.VERSION_CODES.N)
    private void logisticsGoods(ItemViewHolder holder, T t) {
        holder.setBinding(BR.viewModel, t);
        MemberLogisticsEntity entity = (MemberLogisticsEntity) t;
        ItemOtherLogistics2Binding binding = (ItemOtherLogistics2Binding) holder.getmBinding();
        binding.tvCity.setText(String.format("%s %s ➞ %s %s", entity.getCityName(),
                entity.getDistrictName(), entity.getDestCityName(), entity.getDestDistrictName()));

        // 显示竞价按钮
//        if (entity.getPattern() == 1) {
            if (!TextUtils.isEmpty(entity.getLowestPrice())) {
                DecimalFormat df = new DecimalFormat("#.00");
                binding.tvLowestPrice.setText(df.format(entity.getLowestPrice())+"元");
            }else {
                binding.tvLowestPrice.setText("/");
            }
//        }
       // binding.tvLowestPrice.setText(Html.fromHtml("<font>最低报价 </font>" + "<font color='#000000'>" + NumberUtil.trim0(entity.getLowestPrice()) + "元/吨" + "</font>"));
        if (entity.getCarNumber()>0) {
            binding.carNumber.setText(entity.getCarNumber());
        }else {
            binding.carNumber.setText("0");
        }

        // 距您 44.7km
/*        if (TextUtils.isEmpty(entity.getJourney())) {
            binding.tvDistance.setText("");
            binding.tvDistance0.setText("");
        } else {
            binding.tvDistance.setText(String.format("距您 %s", Double.parseDouble(entity.getTotalDistance()) < 1000 ? "1km以内" : StringUtil.fromatUpDistance(entity.getTotalDistance()) + "km"));
            binding.tvDistance0.setText(String.format("距您 %s", Double.parseDouble(entity.getTotalDistance()) < 1000 ? "1km以内" : StringUtil.fromatUpDistance(entity.getTotalDistance()) + "km"));
        }*/

//        binding.tvDistance.setText("");
//        binding.tvDistance0.setText("");

        // 类型名称
        String wightStr = TextUtils.isEmpty(entity.getWeight()) || TextUtils.isDigitsOnly(entity.getWeight()) ? "" : "/" + entity.getWeight() + "吨";
        binding.typeName.setText(String.format("%s%s%s%s%s", entity.getGoodsModeName(), wightStr,
                TextUtils.isEmpty(entity.getVolume()) ? "" : "/" +
                        NumberUtil.trim0(entity.getVolume()) + "方",
                "/"+entity.getPackingModeName(), "/"+entity.getHandleName()));
        // 10/01 12点-7/15 15点 装货
        if (entity.getPattern() == 0) {
            //整车/零担
            String loadTypeStr = MCargoConstant.getTransportTypeStr(entity.getTransportType());
            if (loadTypeStr.equals("零担")) {
                binding.loadType.setVisibility(View.VISIBLE);
                binding.loadType.setImageResource(R.mipmap.member_icon_less_than_tag);
            } else if (loadTypeStr.equals("整车")) {
                binding.loadType.setVisibility(View.VISIBLE);
                binding.loadType.setImageResource(R.mipmap.member_all_cart);
            } else {
                binding.loadType.setVisibility(View.GONE);
            }
            //装货时间调整
            String loadTime = MCargoConstant.stampFormatListLoadTime(
                    entity.getShipmentTime(), entity.getStartPointTime(),
                    entity.getShipmentEndTime(), entity.getEndPointTime());
            if (!TextUtils.isEmpty(loadTime)) {
                binding.loadTime.setText(String.format("%s 装货", loadTime));
            } else {
                binding.loadTime.setVisibility(View.GONE);
            }

        } else {
            binding.loadType.setVisibility(View.GONE);
            //装货时间调整
            String loadTime = MCargoConstant.stampFormatListLoadTime(
                    entity.getShipmentTime(), entity.getStartPointTime(),
                    entity.getShipmentEndTime(), entity.getEndPointTime());
            if (!TextUtils.isEmpty(loadTime)) {
                binding.loadTime.setText(String.format("%s 装货", loadTime));
            } else {
                binding.loadTime.setVisibility(View.GONE);
            }
        }

        // 企业/团队/个人（司机）可装货
        String carModelNameStr = TextUtils.isEmpty(entity.getCarModelName()) ? "" : entity.getCarModelName();
        String carLenStr = TextUtils.isEmpty(entity.getCarLenName()) ? "" : " " + entity.getCarLenName();
        String heightHurdleStr = getValidHeightHurdle(entity.getHeightHurdle());
        String dumpStr = TextUtils.isEmpty(MVehicleConstant.getIsDumpStr(entity.getIsDump())) ? "" : " " + MVehicleConstant.getIsDumpStr(entity.getIsDump());
        if (entity.getPattern() == 1) {
            binding.contentView.setText(TextUtils.isEmpty(entity.getOfferType()) ? "" : entity.getOfferTypeWorlds() + " 可报价");
        } else {
            binding.contentView.setText(String.format("%s%s%s%s", carModelNameStr, carLenStr, heightHurdleStr,"/"+ dumpStr));
        }

        // 运费 40000元
        // 一口价/电议
        if (entity.getFreightType() == 1) {
            binding.priceView.setText("电议");
        } else {
            binding.priceView.setText(!entity.getAllFreight().isEmpty() ? "运费 " + NumberUtil.trim0(entity.getAllFreight()) + "元" : "");
        }
        // 运程 33.33km
//        if (TextUtils.isEmpty(entity.getTotalDistance())) {
//            binding.transportDistance.setText("");
//            binding.transportDistance.setVisibility(View.GONE);
//        } else {
//            binding.transportDistance.setVisibility(entity.getPattern() == 1 ? View.GONE : View.VISIBLE);
//            String journey = Double.parseDouble(entity.getTotalDistance()) < 1000 ? "1km以内" : StringUtil.fromatUpDistance(entity.getTotalDistance()) + "km";
//            binding.transportDistance.setText(String.format("运程 %s", journey));
//        }

        binding.carNumber.setOnClickListener(v -> {
            if (TextUtils.isEmpty(SharedPreferenceUtil.getInstance().
                    getSaveStringData(SharedPreferenceUtil.LOGIN_TOKEN, ""))) {
                MemberConfig.getInstance().startVerificationCodeLogin(context, 0);
            } else {
                if (MemberConfig.getInstance().isCompany() && MemberConfig.getInstance().getTeamOrCompanyNameNo().equals(entity.getCompanyNo()) && entity.getTerminal().equals("1")) {
                    //当前是企业并且跟货源发布人一致
                    MemberConfig.getInstance().startQuotedOwnerActivity(context, entity.getNo());
                } else if (MemberConfig.getInstance().isTeam() && MemberConfig.getInstance().getTeamOrCompanyNameNo().equals(entity.getTeamNo()) && entity.getTerminal().equals("2")) {
                    //当前是团队并且跟货源发布人一致
                    MemberConfig.getInstance().startQuotedOwnerActivity(context, entity.getNo());
                } else if (MemberConfig.getInstance().isPersonal() && MemberConfig.getInstance().getUserNo().equals(entity.getUserNo()) && entity.getTerminal().equals("3")) {
                    //当前是个人并且跟货源发布人一致
                    MemberConfig.getInstance().startQuotedOwnerActivity(context, entity.getNo());
                } else {
                    MemberConfig.getInstance().startQuotedOwnerActivity(context, entity.getNo());
                }
            }
        });


        binding.callPhone.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!TextUtils.isEmpty(entity.getContrctsNumber()))
                    showDialog(entity.getContrctsNumber());
                else
                    MToast.showLongToast(v.getContext(), "无联系方式");
            }
        });
        if (!TextUtils.isEmpty(entity.getCompanyName())) {
            binding.tvCompany.setText(entity.getCompanyName());
        }
//        if (!TextUtils.isEmpty(entity.getTerminal())) {
//            binding.role.setVisibility(View.VISIBLE);
//            switch (entity.getTerminal()) {
//                case "1":
//                    binding.role.setVisibility(View.VISIBLE);
//                    break;
//                case "2":
//                    //个人不展示
//                case "3":
//                    binding.role.setVisibility(View.GONE);
//                    break;
//            }
//        } else {
//            binding.role.setVisibility(View.GONE);
//        }
    }



    private String getValidHeightHurdle(String heightHurdle) {
        if (TextUtils.isEmpty(heightHurdle) || heightHurdle.equals("0.0")) {
            return "";
        }
        return " 栏高 " + NumberUtil.trim0(heightHurdle) + "米";
    }

    private void showDialog(String phone) {
        View view = View.inflate(context, R.layout.dialog_two_btn, null);
        final RoundCornerDialog roundCornerDialog = new RoundCornerDialog(context, 0, 0, view, R.style.RoundCornerDialog);
        roundCornerDialog.show();
        roundCornerDialog.setCanceledOnTouchOutside(false);// 设置点击屏幕Dialog不消失

        TextView tv_message = view.findViewById(R.id.tv_message);
        TextView tv_title = view.findViewById(R.id.tv_title);
        TextView tv_logout_confirm = view.findViewById(R.id.tv_logout_confirm);
        TextView tv_logout_cancel = view.findViewById(R.id.tv_logout_cancel);
        tv_title.setText("确认呼叫");
        tv_title.setVisibility(View.VISIBLE);
        tv_message.setText(phone);
        tv_logout_confirm.setText("拨打");
        //确定
        tv_logout_confirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(Intent.ACTION_DIAL);
                Uri data = Uri.parse("tel:" + phone);
                intent.setData(data);
                context.startActivity(intent);
                roundCornerDialog.dismiss();

            }
        });
        //取消
        tv_logout_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                roundCornerDialog.dismiss();
            }
        });
    }
}
